package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.ExStorageFeeBiz;
import com.egao.orderB.entity.ExStorageFee;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-03-14 16:04:20
 * @version ：1.0
 */
@Repository
public class ExStorageFeeBizImpl extends BaseDao implements ExStorageFeeBiz {

	@Override
	public JsonResult list(PageResult2<T> page, ExStorageFee exStorageFee, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<ExStorageFee> rm = new BeanPropertyRowMapper<ExStorageFee>(ExStorageFee.class);
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.ex_storage_id");
    sql.append(",e.type,e.fee_name,e.price,e.fee_state");
		sql.append(",e.fee_person,e.fee_time,e.fobject,e.fee_modules");
		sql.append(",e.invoiceid,e.invoicename,e.driver_name,e.invoicenum");
		sql.append(",e.invoicestate,e.currency,e.exchange_rate,e.settlement");
		sql.append(",e.settlement_price,e.remark,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_ex_storage_fee e ");
    sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
    sql.append(" where e.deleted=0 ");
		sql.append(" and e.ex_storage_id=:ex_storage_id ");
		map.put("ex_storage_id", exStorageFee.getExStorageId());

    if(Utils.isNotEmpty(exStorageFee)){
			if(Utils.isNotEmpty(exStorageFee.getInvoicename())){
				sql.append(" and instr(e.invoicename,:invoicename)>0 ");
				map.put("invoicename", exStorageFee.getInvoicename());
			}
      if(Utils.isNotEmpty(exStorageFee.getSort())&&Utils.isNotEmpty(exStorageFee.getOrder())){
        sql.append(" order by e."+exStorageFee.getSort()+" "+exStorageFee.getOrder());
      } else {
        sql.append(" order by e.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<ExStorageFee> list(ExStorageFee exStorageFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<ExStorageFee> rm = new BeanPropertyRowMapper<ExStorageFee>(ExStorageFee.class);
    StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.ex_storage_id");
    sql.append(",e.type,e.fee_name,e.price,e.fee_state");
		sql.append(",e.fee_person,e.fee_time,e.fobject,e.fee_modules");
		sql.append(",e.invoiceid,e.invoicename,e.driver_name,e.invoicenum");
		sql.append(",e.invoicestate,e.currency,e.exchange_rate,e.settlement");
		sql.append(",e.settlement_price,e.remark,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_ex_storage_fee e ");
    sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
    sql.append(" where e.deleted=0 ");
		sql.append(" and e.ex_storage_id=:ex_storage_id ");
		map.put("ex_storage_id", exStorageFee.getExStorageId());

    if(Utils.isNotEmpty(exStorageFee)){
			if(Utils.isNotEmpty(exStorageFee.getInvoicename())){
				sql.append(" and instr(e.invoicename,:invoicename)>0 ");
				map.put("invoicename", exStorageFee.getInvoicename());
			}
      if(Utils.isNotEmpty(exStorageFee.getSort())&&Utils.isNotEmpty(exStorageFee.getOrder())){
        sql.append(" order by e."+exStorageFee.getSort()+" "+exStorageFee.getOrder());
      } else {
        sql.append(" order by e.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public ExStorageFee getById(ExStorageFee exStorageFee) {
		RowMapper<ExStorageFee> rm = new BeanPropertyRowMapper<ExStorageFee>(ExStorageFee.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select e.id,e.organization_id,e.ex_storage_id");
    sql.append(",e.type,e.fee_name,e.price,e.fee_state");
		sql.append(",e.fee_person,e.fee_time,e.fobject,e.fee_modules");
		sql.append(",e.invoiceid,e.invoicename,e.driver_name,e.invoicenum");
		sql.append(",e.invoicestate,e.currency,e.exchange_rate,e.settlement");
		sql.append(",e.settlement_price,e.remark,e.create_time,e.create_user");
		sql.append(",e.update_time,e.update_user");
    sql.append(",suser.nick_name createName ");
    sql.append(" from chf_ex_storage_fee e ");
    sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
    sql.append(" where e.deleted=0 and e.id=:id ");
		map.put("id", exStorageFee.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(ExStorageFee exStorageFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ex_storage_fee (");
    sql.append("organization_id,ex_storage_id,type,fee_name");
		sql.append(",price,fee_state,fee_person,fee_time");
		sql.append(",fobject,fee_modules,invoiceid,invoicename");
		sql.append(",driver_name,invoicenum,invoicestate,currency");
		sql.append(",exchange_rate,settlement,settlement_price,remark");
		sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:ex_storage_id,:type,:fee_name");
		sql.append(",:price,:fee_state,:fee_person,:fee_time");
		sql.append(",:fobject,:fee_modules,:invoiceid,:invoicename");
		sql.append(",:driver_name,:invoicenum,:invoicestate,:currency");
		sql.append(",:exchange_rate,:settlement,:settlement_price,:remark");
		sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("ex_storage_id", exStorageFee.getExStorageId());
    map.put("type", exStorageFee.getType());
    map.put("fee_name", exStorageFee.getFeeName());
    map.put("price", exStorageFee.getPrice());
    map.put("fee_state", exStorageFee.getFeeState());
    map.put("fee_person", exStorageFee.getFeePerson());
    map.put("fee_time", exStorageFee.getFeeTime());
    map.put("fobject", exStorageFee.getFobject());
    map.put("fee_modules", exStorageFee.getFeeModules());
    map.put("invoiceid", exStorageFee.getInvoiceid());
    map.put("invoicename", exStorageFee.getInvoicename());
    map.put("driver_name", exStorageFee.getDriverName());
    map.put("invoicenum", exStorageFee.getInvoicenum());
    map.put("invoicestate", exStorageFee.getInvoicestate());
    map.put("currency", exStorageFee.getCurrency());
    map.put("exchange_rate", exStorageFee.getExchangeRate());
    map.put("settlement", exStorageFee.getSettlement()==null?null:exStorageFee.getSettlement()+"");
    map.put("settlement_price", exStorageFee.getSettlementPrice());
    map.put("remark", exStorageFee.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(ExStorageFee exStorageFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ex_storage_fee set ");
    sql.append("fee_name=:fee_name ");
		sql.append(",price=:price,fee_state=:fee_state,fee_person=:fee_person ");
		sql.append(",fee_time=:fee_time,fobject=:fobject,fee_modules=:fee_modules ");
		sql.append(",invoiceid=:invoiceid,invoicename=:invoicename,driver_name=:driver_name ");
		sql.append(",invoicenum=:invoicenum,invoicestate=:invoicestate,currency=:currency ");
		sql.append(",exchange_rate=:exchange_rate,settlement=:settlement,settlement_price=:settlement_price ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
    map.put("fee_name", exStorageFee.getFeeName());
    map.put("price", exStorageFee.getPrice());
    map.put("fee_state", exStorageFee.getFeeState());
    map.put("fee_person", exStorageFee.getFeePerson());
    map.put("fee_time", exStorageFee.getFeeTime());
    map.put("fobject", exStorageFee.getFobject());
    map.put("fee_modules", exStorageFee.getFeeModules());
    map.put("invoiceid", exStorageFee.getInvoiceid());
    map.put("invoicename", exStorageFee.getInvoicename());
    map.put("driver_name", exStorageFee.getDriverName());
    map.put("invoicenum", exStorageFee.getInvoicenum());
    map.put("invoicestate", exStorageFee.getInvoicestate());
    map.put("currency", exStorageFee.getCurrency());
    map.put("exchange_rate", exStorageFee.getExchangeRate());
    map.put("settlement", exStorageFee.getSettlement()==null?null:exStorageFee.getSettlement()+"");
    map.put("settlement_price", exStorageFee.getSettlementPrice());
    map.put("remark", exStorageFee.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", exStorageFee.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(ExStorageFee exStorageFee, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(exStorageFee.getId().length()>0){
			for(String ID : exStorageFee.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ex_storage_fee set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
